﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ClassCommon;
using ClassModel;
using System.Data.SqlClient;
using System.Data;

namespace ClassDAL
{
    public class t_class_task_DAL
    {

        /// <summary>
        /// 插入排课任务
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public static int InsertTask(t_class_task task)
        {
            /**
             * INSERT INTO t_class_task
	(id,userid,name,type,status,createtime,school)
VALUES
	();
SELECT @@IDENTITY;

             */

            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.AppendLine("INSERT INTO t_class_task");
            sqlBuilder.AppendLine("     (userid,name,type,status,createtime,school)");
            sqlBuilder.AppendLine("VALUES ");
            sqlBuilder.AppendLine("     (@userid,@name,@type,@status,@createtime,@school);");
            sqlBuilder.AppendLine("SELECT @@IDENTITY;");

            SqlParameter[] insertParams = new SqlParameter[] 
            {
                new SqlParameter("@userid",task.UserId),
                new SqlParameter("@name",task.Name),
                new SqlParameter("@type",task.Type),
                new SqlParameter("@status",task.Type),
                new SqlParameter("@createtime",task.CreateTime),
                new SqlParameter("@school",task.School)
            };



            return Convert.ToInt32(SQLHelper.ExecuteScalar(SQLHelper.GetConnection(), System.Data.CommandType.Text, sqlBuilder.ToString(), insertParams));

        }

        /// <summary>
        /// 返回用户的排课任务列表
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public static IList<t_class_task> List(int userid, int pageIndex = 0, int pageCount = 10)
        {
            string listSQL = @"select * from 
                                (select id,userid,name,[type],[status],createtime,school,ROW_NUMBER() over (order by createtime                                     desc) as rn
	                            from t_class_task where userid = @userid) tmp
                                where rn between @begin and @end;";

            int begin = pageIndex * pageCount + 1;
            int end = pageCount * pageCount + pageCount;

            SqlParameter[] listParams = new SqlParameter[] 
            {
                new SqlParameter("@userid",userid),
                new SqlParameter("@begin",begin),
                new SqlParameter("@end",end)
            };

            DataSet ds = SQLHelper.ExecuteDataset(SQLHelper.GetConnection(), CommandType.Text, listSQL, listParams);

            if (ds.Tables == null || ds.Tables.Count == 0) return null;

            List<t_class_task> tasks = new List<t_class_task>();
            DataRowCollection rows = ds.Tables[0].Rows;
            foreach (DataRow row in rows)
            {
                t_class_task task = new t_class_task();

                task.Id = Convert.ToInt32(row["id"]);
                task.UserId = Convert.ToInt32(row["userid"]);
                task.Name = row["name"].ToString();
                task.Status = row["status"].ToString();
                task.CreateTime = Convert.ToDateTime(row["createtime"]);
                task.School = row["school"].ToString();
                

                tasks.Add(task);
            }


            return tasks;
        }


        /// <summary>
        /// 删除排课任务
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static int DeleteById(int id, int userid)
        {
            string deleteSQL = "DELETE FROM t_class_task where id = @id and userid = @userid;";

            SqlParameter[] deleteParams = new SqlParameter[] 
            {
                new SqlParameter("@id",id),
                new SqlParameter("@userid",userid)
            };

            return SQLHelper.ExecuteNonQuery(SQLHelper.GetConnection(), CommandType.Text, deleteSQL, deleteParams);
        }

        /// <summary>
        /// 获取用户任务信息
        /// </summary>
        /// <param name="taskId"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public static t_class_task GetTaskById(int taskid, int userid)
        {
            string selectSQL = "select id,userid,name,type,status,createtime,school from t_class_task where id = @taskid and userid = @userid;";

            SqlParameter[] selectParams = new SqlParameter[] 
            {
                new SqlParameter("@taskid",taskid),
                new SqlParameter("@userid",userid)
            };

            SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.GetConnection(), CommandType.Text, selectSQL, selectParams);

            if (!reader.Read()) return null;

            t_class_task task = new t_class_task();
            task.Id = Convert.ToInt32(reader["id"]);
            task.UserId = Convert.ToInt32(reader["userid"]);
            task.Name = reader["name"].ToString();
            task.Type = reader["type"].ToString();
            task.Status = reader["status"].ToString();
            task.CreateTime = Convert.ToDateTime(reader["createtime"]);
            task.School = reader["school"].ToString();

            reader.Close();

            return task;
        }

    }
}
